package com.dream.keigezhushou.Activity.acty.listen;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import co.lujun.androidtagview.TagContainerLayout;
import co.lujun.androidtagview.TagView;

public class FaPingLunActivity extends AppCompatActivity {


    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fapinglun_title)
    RelativeLayout fapinglunTitle;
    @BindView(R.id.tagcontainerLayout1)
    TagContainerLayout tagcontainerLayout1;
    @BindView(R.id.text_pinglun)
    EditText textPinglun;
    @BindView(R.id.tv_addtext)
    TextView tvAddtext;
    @BindView(R.id.rl_add_text)
    RelativeLayout rlAddText;
    @BindView(R.id.activity_fa_ping_lun)
    RelativeLayout activityFaPingLun;
    private int listSize;
    private TagContainerLayout mTagContainerLayout1;
    private ArrayList<String> list1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fa_ping_lun);


        ButterKnife.bind(this);
        mylist();

        initData();


    }

    private void mylist() {
        list1 = new ArrayList<String>();
        list1.add("Java");
        list1.add("C++");
        list1.add("Python");
        list1.add("Swift");
        list1.add("你好，这是一个TAG。你好，这是一个TAG。你好，这是一个TAG。你好，这是一个TAG。");
        list1.add("PHP");
        list1.add("JavaScript");
        list1.add("Html");
        list1.add("Welcome to use AndroidTagView!");
    }

    private void initData() {

        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTagContainerLayout1 = (TagContainerLayout) findViewById(R.id.tagcontainerLayout1);


        mTagContainerLayout1.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(int position, String text) {

                if (text.equals("点击添加")) {
                    Toast.makeText(FaPingLunActivity.this, "添加标签按钮",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(FaPingLunActivity.this, "click-position:" + position + ", text:" + text,
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onTagLongClick(final int position, String text) {
                AlertDialog dialog = new AlertDialog.Builder(FaPingLunActivity.this)
                        .setTitle("long click")
                        .setMessage("You will delete this tag!")
                        .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mTagContainerLayout1.removeTag(position);
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                dialog.show();
            }

            @Override
            public void onTagCrossClick(int position) {

                if (position == listSize) {
                    Toast.makeText(FaPingLunActivity.this, "添加控件无法删除 " + position,
                            Toast.LENGTH_SHORT).show();
                } else {
                    mTagContainerLayout1.removeTag(position);
                    --listSize;
                    Toast.makeText(FaPingLunActivity.this, "删除 " + position,
                            Toast.LENGTH_SHORT).show();
                }

            }
        });


        mTagContainerLayout1.setTags(list1);
        listSize = list1.size();

        mTagContainerLayout1.addTag("点击添加");
        rlAddText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mTagContainerLayout1.addTag(textPinglun.getText().toString(), listSize);
                ++listSize;
                // Add tag in the specified position
//                mTagContainerLayout1.addTag(text.getText().toString(), 4);
            }
        });

    }
}
